package cz.anime.ppro.beans;

import java.io.IOException;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;

import cz.anime.ppro.logic.FragmentComponent;
import cz.anime.ppro.model.Content;
import cz.anime.ppro.model.Fragment;


@Component
@ManagedBean
@Scope(WebApplicationContext.SCOPE_REQUEST)
public class FragmentAdministrationBean {

	private List<Fragment> fragments;
	
	@Autowired
	private FragmentComponent fragmentComponent;

	@PostConstruct
	public void init(){
		fragments = fragmentComponent.getFragements();
	}
	
	public void deleteFragment(int fragmentId){
		
		Fragment fragment = fragmentComponent.getFragment(fragmentId);
		
		fragmentComponent.delete(fragment);
		getFragments().remove(fragment);
		
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect("/PPRO2/admin-fragments.jsf");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public List<Fragment> getFragments() {
		return fragments;
	}

	public void setFragments(List<Fragment> fragments) {
		this.fragments = fragments;
	}
		
}
